package bairui.international.weixin.portal.miniapp.controller.prize.nine;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.data.jpa.ListSortResult;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_Prize;
import bairui.international.model.domain.jpa.TB_PrizeItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.domain.jpa.TB_WinPrize;
import bairui.international.model.domain.jpa.id.ID_WinPrize;
import bairui.international.model.status.State_Prize;
import bairui.international.service.Service_Prize;
import bairui.international.service.Service_PrizeItem;
import bairui.international.service.Service_User;
import bairui.international.service.Service_WinPrize;
import lombok.Getter;

/**
 *
 * 获取
 *
 * @generated
 * 
 * @see STest_C_Prize_Nine_Get_Get 测试
 *
 */
@Controller
public class C_Prize_Nine_Get_Get
 extends SC_Prize_Nine_Get_Get 
{
	
	@Autowired private Service_Prize service_Prize;
	@Autowired private Service_PrizeItem service_PrizeItem;
	@Autowired private Service_WinPrize service_WinPrize;
	@Autowired private Service_User service_User;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam String skey
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		TB_Prize prize = service_Prize.findFirst(it->it.state.eq(State_Prize.Uped), it->new QSort(it.orderValue.desc()));
		if(null == prize){
			return null;
		}
		ListSortResult<TB_PrizeItem> list = service_PrizeItem.list(it->it.prize.eq(prize), it->new QSort(it.orderValue.desc()));
		//
		TB_WinPrize winPrize = service_WinPrize.findOne(it->it.id.eq(new ID_WinPrize(user, prize)));
		//
		return new Output(prize, list.getContent(), winPrize);
	}
	
	public static class Output {
		private @Getter long id;
		private @Getter String title;
		private @Getter String content;
		private @Getter String backgroundImg;
		private @Getter List<ItemOutput> item;
		private @Getter WinOutput winPrize;	// 中奖数据

		public Output(TB_Prize prize, List<TB_PrizeItem> list, TB_WinPrize winPrize) {
			this.id = prize.getId();
			this.title = prize.getTitle();
			this.content = prize.getContent();
			this.backgroundImg = C_Prize_Nine_ShowBackground_ShowImg.ShowImg.Operate(prize.getId()).getUrl();
			this.item = list.stream().map(it->new ItemOutput(it)).collect(Collectors.toList());
			if(null != winPrize){
				this.winPrize = new WinOutput(winPrize);
			}
		}
		
	}
	
	public static class ItemOutput {
		private @Getter long id;
		private @Getter String name;
		private @Getter String img;

		public ItemOutput(TB_PrizeItem it) {
			this.id = it.getId();
			this.name = it.getPrizeName();
			this.img = C_Prize_Nine_ItemShow_ShowImg.ShowImg.Operate(it.getId()).getUrl();
		}
	}
	
	public static class WinOutput {
		private @Getter String name;
		private @Getter String img;
		private @Getter Boolean noPrizeFlag;

		public WinOutput(TB_WinPrize winPrize) {
			this.name = winPrize.getPrizeName();
			this.img = C_Prize_Nine_WinPrizeShow_ShowImg.ShowImg.Operate(winPrize.getId()).getUrl();
			this.noPrizeFlag = winPrize.getNoPrizeFlag();
		}
	}
	
}
